package com.netcracker.ui.listener;

import com.netcracker.ui.helper.UIHelper;
import com.netcracker.ui.command.AddAuthorCommand;
import com.netcracker.ui.command.NewPublisherCommand;
import com.netcracker.ui.command.UpdatePublisherCommand;
import com.netcracker.ui.command.ChangeUserInfoCommand;
import com.netcracker.ui.command.ChangeUserPasswordCommand;
import com.netcracker.ui.command.RegistrationCommand;
import com.netcracker.ui.command.FindBookCommand;
import com.netcracker.ui.command.DeleteAuthorCommand;
import com.netcracker.ui.command.GetAllGenresCommand;
import com.netcracker.ui.command.AddGenreCommand;
import com.netcracker.ui.command.DeleteBookCommand;
import com.netcracker.ui.command.ReadBookCommand;
import com.netcracker.ui.command.ReadGenreCommand;
import com.netcracker.ui.command.GetAllUsersCommand;
import com.netcracker.ui.command.NewGenreCommand;
import com.netcracker.ui.command.UpdateGenreCommand;
import com.netcracker.ui.command.SearchCommand;
import com.netcracker.ui.command.DeletePublisherCommand;
import com.netcracker.ui.command.UpdateBookCommand;
import com.netcracker.ui.command.GetAllAuthorsCommand;
import com.netcracker.ui.command.GetAllCountriesCommand;
import com.netcracker.ui.command.AddBookCommand;
import com.netcracker.ui.command.DeleteGenreCommand;
import com.netcracker.ui.command.UpdateAuthorCommand;
import com.netcracker.ui.command.FindAuthorCommand;
import com.netcracker.ui.command.EditProfileCommand;
import com.netcracker.ui.command.GetAllPublishersCommand;
import com.netcracker.ui.command.ReadAuthorCommand;
import com.netcracker.ui.command.Command;
import com.netcracker.ui.command.NewBookCommand;
import com.netcracker.ui.command.GetAllBooksCommand;
import com.netcracker.ui.command.NewAuthorCommand;
import com.netcracker.ui.command.AuthorizationCommand;
import com.netcracker.ui.command.DeleteUserCommand;
import com.netcracker.ui.command.AddPublisherCommand;
import com.netcracker.ui.command.ViewProfileCommand;
import com.netcracker.db.exception.DBException;
import com.netcracker.ui.ViewModel;
import com.netcracker.util.service.ServiceFacade;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class UIServletContextListener implements ServletContextListener {
        private final ViewModel viewModel = new ViewModel();
        
        private Map<String, Command> raiseCommands(ServiceFacade serviceFacade) {
            Map<String, Command> commands = new HashMap<>();
            commands.put("allAuthors", new GetAllAuthorsCommand(serviceFacade));
            commands.put("allBooks", new GetAllBooksCommand(serviceFacade));
            commands.put("allGenres", new GetAllGenresCommand(serviceFacade));
            commands.put("allUsers", new GetAllUsersCommand(serviceFacade));
            commands.put("allPublishers", new GetAllPublishersCommand(serviceFacade));
            commands.put("allCountries", new GetAllCountriesCommand(serviceFacade));
            commands.put("addAuthor", new AddAuthorCommand(serviceFacade));
            commands.put("addBook", new AddBookCommand(serviceFacade));
            commands.put("addGenre", new AddGenreCommand(serviceFacade));
            commands.put("addPublisher", new AddPublisherCommand(serviceFacade));
            commands.put("changeUserInfo", new ChangeUserInfoCommand(serviceFacade));
            commands.put("changeUserPass", new ChangeUserPasswordCommand(serviceFacade));
            commands.put("deleteAuthor", new DeleteAuthorCommand(serviceFacade));
            commands.put("deleteBook", new DeleteBookCommand(serviceFacade));
            commands.put("deleteGenre", new DeleteGenreCommand(serviceFacade));
            commands.put("deletePublisher", new DeletePublisherCommand(serviceFacade));
            commands.put("deleteUser", new DeleteUserCommand(serviceFacade));
            commands.put("editProfile", new EditProfileCommand(serviceFacade));           
            commands.put("findBook", new FindBookCommand(serviceFacade));
            commands.put("findAuthor", new FindAuthorCommand(serviceFacade));             // not yet
            commands.put("login", new AuthorizationCommand(serviceFacade));
            commands.put("newBook", new NewBookCommand(serviceFacade));
            commands.put("newAuthor", new NewAuthorCommand(serviceFacade));
            commands.put("newGenre", new NewGenreCommand(serviceFacade));
            commands.put("newPublisher", new NewPublisherCommand(serviceFacade));
            commands.put("readAuthor", new ReadAuthorCommand(serviceFacade));
            commands.put("readBook", new ReadBookCommand(serviceFacade));
            commands.put("readGenre", new ReadGenreCommand(serviceFacade)); 
            commands.put("registration", new RegistrationCommand(serviceFacade));
            commands.put("search", new SearchCommand(serviceFacade));
            commands.put("updateAuthor", new UpdateAuthorCommand());  // not yet
            commands.put("updateBook", new UpdateBookCommand());      // not yet
            commands.put("updateGenre", new UpdateGenreCommand());    //   not yet
            commands.put("updatePublisher", new UpdatePublisherCommand());
            commands.put("viewProfile", new ViewProfileCommand(serviceFacade));    
            return commands;
    }
    
        
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServiceFacade serviceFacade = viewModel.getServiceFacade();  
        UIHelper.setMapCommands(raiseCommands(serviceFacade));
        UIHelper.setSericeFacade(serviceFacade);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            viewModel.getDBManager().close();
        } catch (DBException ex) {
            
        }
    }
    
}
